package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.itheima.constant.MessageConstant;
import com.itheima.dao.FamiliesDao;
import com.itheima.dao.MemberDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Families;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;

import com.itheima.service.RegisterService;
import com.itheima.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RegisterService.class)
@Transactional
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private FamiliesDao familiesDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result register(@RequestBody Map map) {
        String telephone = (String) map.get("telephone");//获取用户页面输入的手机号
        Member member = memberDao.findByTelephone(telephone);
        // 4、检查当前用户是否为会员
        if (member != null) {
            return new Result(false, MessageConstant.Register_FAIL);
        } else {

            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            member.setPassword(MD5Utils.md5((String) map.get("password")));
            //自动完成会员注册
            memberDao.add(member);

            Families families = new Families();
            families.setIdCard((String) map.get("idCard"));
            families.setSex((String) map.get("sex"));
            families.setName((String) map.get("name"));
            familiesDao.add(families);
            Integer famID = familiesDao.findByCid(families);
            Integer memID = member.getId();
            Map<String, Integer> map1 = new HashMap<>();
            map1.put("memID", memID);
            map1.put("famID", famID);
            familiesDao.setFM(map1);
        }
        return new Result(true, MessageConstant.Register_SUCCESS);
    }
}